import axios, { HttpResponse } from '@/api/interceptor';
import { MenuData, MenuResp, MenuTree } from '@/api/menu/types';
import { AxiosResponse } from 'axios';

export const selectMenuList = async (
  params: any,
): Promise<HttpResponse<MenuResp[]>> => {
  const result: AxiosResponse<HttpResponse<MenuResp[]>> = await axios({
    url: '/menu/listPage',
    method: 'GET',
    params,
  });
  return result.data;
};

export const getMenuTree = async (
  requireButton: boolean,
): Promise<HttpResponse<MenuTree[]>> => {
  const result: AxiosResponse<HttpResponse<MenuTree[]>> = await axios({
    url: '/menu/getMenuTree',
    method: 'GET',
    params: { requireButton },
  });
  return result.data;
};

export const createMenu = async (menuData: MenuData): Promise<HttpResponse> => {
  const result: AxiosResponse<HttpResponse> = await axios({
    url: '/menu/createMenu',
    method: 'POST',
    data: menuData,
  });
  return result.data;
};

export const removeMenu = async (ids: number[]): Promise<HttpResponse> => {
  const result: AxiosResponse<HttpResponse> = await axios({
    url: '/menu/removeMenu',
    method: 'DELETE',
    data: ids,
  });
  return result.data;
};

export const getMenuDetail = async (
  id: number,
): Promise<HttpResponse<MenuData>> => {
  const result: AxiosResponse<HttpResponse<MenuData>> = await axios({
    url: '/menu/detail',
    method: 'GET',
    params: { id },
  });
  return result.data;
};

export const editMenu = async (menuData: MenuData): Promise<HttpResponse> => {
  const result: AxiosResponse<HttpResponse> = await axios({
    url: '/menu/editMenu',
    method: 'PUT',
    data: menuData,
  });
  return result.data;
};
